package com.shop.admin.service.impl;

import com.shop.Result.LdbzResult;
import com.shop.admin.service.AdminIndexSlideAdService;
import com.shop.api.feign.IndexSlideAdFeign;
import com.shop.pojo.LdbzIndexSlideAd;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@GlobalTransactional
public class AdminIndexSlideAdServiceImpl implements AdminIndexSlideAdService {

    private static final Logger logger = LoggerFactory.getLogger(AdminIndexSlideAdServiceImpl.class);

    @Autowired
    private IndexSlideAdFeign indexSlideAdFeign;

    @Override
    public LdbzResult countIndexSlideAd(LdbzIndexSlideAd entity) {
        return LdbzResult.ok(indexSlideAdFeign.countByEntity(entity));
    }

    @Override
    public LdbzResult getIndexSlideAdPage(LdbzIndexSlideAd entity, int pn, int limit) {
        Map<String,Object> map = new HashMap<>();
        long l = indexSlideAdFeign.countByEntity(entity);
        map.put("total",l);
        if (l>0&&pn>0){
            int start = (pn-1)*limit;
            List<LdbzIndexSlideAd> indexSlideAdPage = indexSlideAdFeign.getIndexSlideAdPage(entity, start, limit);
            map.put("list",indexSlideAdPage);
        }
        return LdbzResult.build(0,"",map);
    }

    @Override
    public LdbzResult selectByKey(Long id) {
        return LdbzResult.ok(indexSlideAdFeign.selectByKey(id));
    }

    @Override
    public LdbzResult deleteByKey(String id) {

        if (StringUtils.contains(id, ",")) {
            List<Long> ids = new ArrayList<>();
            for (String _id : StringUtils.split(id, ",")) {
                ids.add(Long.valueOf(_id));
            }
            return LdbzResult.ok(indexSlideAdFeign.deleteByKeys(ids));
        } else {
            return LdbzResult.ok(indexSlideAdFeign.deleteByKey(Long.valueOf(id)));
        }
    }

    @Override
    public LdbzResult insertByEntity(LdbzIndexSlideAd entity) {
        if (indexSlideAdFeign.insertByEntity(entity)>=1){
            return LdbzResult.ok();
        }else {
            return LdbzResult.build(500,"添加失败");
        }
    }

    @Override
    public LdbzResult updateByKey(LdbzIndexSlideAd entity) {
        return LdbzResult.ok(indexSlideAdFeign.updateByKey(entity));
    }
}
